<template>
  <div id="">
    <van-list
    :immediate-check="false"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- <van-cell :title="item.content" /> -->
    <CommentItem @cliclReply="$emit('cliclReply', $event)" v-for="item in list" :key="item.com_id + ''" :row="item" />
    </van-list>
  </div>
</template>

<script>
import { getCommentsList } from '@/api/article.js'
import CommentItem from './CommentItem.vue'
export default {
  name: 'CommentList',
  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Number, Object, String],
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  components: {
    CommentItem
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  created () {
    this.onLoad()
  },
  // 定义方法
  methods: {
    async onLoad () {
      // 异步更新数据
      // 1. 发请求
      const { data: res } = await getCommentsList({
        type: this.type,
        source: this.source + '',
        offset: this.offset,
        limit: this.limit
      })
      this.$emit('updataTotal', res.data.total_count)
      // 2. 存数据
      this.list.push(...res.data.results)
      // 3. 关loading
      this.loading = false
      // 4. 判断是否还有数据 没有则 finished
      if (!res.data.last_id || this.list.length >= res.data.total_count) {
        this.finished = true
      } else {
        this.offset = res.data.last_id
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
